using Newtonsoft.Json;

namespace Mafool.Core;

public static partial class Extends
{
    /// <summary>
    /// 对象转Json字符串
    /// </summary>
    public static string Json(this object o)
    {
        return JsonConvert.SerializeObject(o);
    }

    /// <summary>
    /// 对象转Json字符串(美化输出)
    /// </summary>
    public static string JsonPretty(this object o)
    {
        return JsonConvert.SerializeObject(o, new JsonSerializerSettings
        {
            Formatting = Formatting.Indented
        });
    }
    
    
    /// <summary>
    /// Json字符串转对象
    /// </summary>
    public static T Object<T>(this string json) where T : class
    {
        return JsonConvert.DeserializeObject<T>(json);
    }
}